package com.cskaoyan.shopping.api;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther cskaoyan
 */
@RestController
public class OrderApiImpl {

    @Autowired
    IProductService productService;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ICartService iCartService;

    @DeleteMapping(value = "/rpc/cart/items")
    ClearCartItemServiceResponse clearCartItemByUserID(@RequestBody ClearCartItemRequest request){
        iCartService.deleteCartItemOfOrder(request);
        ClearCartItemServiceResponse response = new ClearCartItemServiceResponse();
        response.setCode("200");
        response.setMsg("删除成功");
        return response;
    }


    @GetMapping(value = "/rpc/product/detail")
    ServiceCallResponse<ProductDetailDto> getProductDetail(@RequestParam("id") Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        // 服务调用的返回值
        ServiceCallResponseUtil<ProductDetailDto> responseUtil = new ServiceCallResponseUtil<>();
        return responseUtil.ok(productDetail);
    }

    @GetMapping(value = "/rpc/items")
    AllItemServiceResponse getAllProductItem(){
        List<Item> items = itemMapper.selectAll();
        List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);
        AllItemServiceResponse allItemServiceResponse = new AllItemServiceResponse();
        allItemServiceResponse.setCallData(itemDtos);
        allItemServiceResponse.setCode("200");
        allItemServiceResponse.setMsg("查询成功");
        return allItemServiceResponse;
    }
}
